package org.dromara.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.SysStoreAreaBo;
import org.dromara.system.domain.vo.SysStoreAreaVo;
import org.dromara.system.domain.SysStoreArea;
import org.dromara.system.mapper.SysStoreAreaMapper;
import org.dromara.system.service.ISysStoreAreaService;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统管理-店内分区配置Service业务层处理
 *
 * @author LionLi
 * @date 2024-03-18
 */
@RequiredArgsConstructor
@Service
public class SysStoreAreaServiceImpl implements ISysStoreAreaService {

    private final SysStoreAreaMapper baseMapper;


    /**
     * 查询系统管理-店内分区配置列表
     */
    @Override
    public TableDataInfo<SysStoreAreaVo> queryPageList(SysStoreAreaBo bo, PageQuery pageQuery) {
        //TODO 通过门店Id或者区域Id获取设备ID集合
       LambdaQueryWrapper<SysStoreArea> lqw = buildQueryWrapper(bo);
        Page<SysStoreAreaVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }


    private LambdaQueryWrapper<SysStoreArea> buildQueryWrapper(SysStoreAreaBo bo) {
        LambdaQueryWrapper<SysStoreArea> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getStoreId() != null, SysStoreArea::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreAreaName()), SysStoreArea::getStoreAreaName, bo.getStoreAreaName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), SysStoreArea::getStatus, bo.getStatus());
        return lqw;
    }


    /**
     * 修改系统管理-店内分区配置
     */
    @Override
    public Boolean updateByBo(SysStoreAreaBo bo) {
        SysStoreArea update = MapstructUtils.convert(bo, SysStoreArea.class);
        return baseMapper.updateById(update) > 0;
    }



    /**
     * 批量删除系统管理-店内分区配置
     */
    @Override
    public Boolean deleteWithValidById(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 批量添加门店分区
     * @param storeAreaBo
     * @return
     */
    @Override
    public Boolean insertList(List<SysStoreAreaBo> storeAreaBo){
        List<SysStoreArea> sysStoreAreas = new ArrayList<>();
        for (SysStoreAreaBo bo : storeAreaBo) {
            SysStoreArea add = MapstructUtils.convert(bo, SysStoreArea.class);
            sysStoreAreas.add(add);
            List<String> deviceIds = bo.getDeviceIds();
           //TODO 调设备那边的微服务
        }
        return  baseMapper.insertBatch(sysStoreAreas);
    }

    /**
     * 校验分区编码是否唯一
     *
     * @param storeAreaBo
     * @return
     */
    @Override
    public boolean checkStoreAreaCode(SysStoreAreaBo storeAreaBo) {

        long storeId = ObjectUtil.isNull(storeAreaBo.getStoreAreaId()) ? -1L : storeAreaBo.getStoreId();
        SysStoreArea store = baseMapper.selectOne(new LambdaQueryWrapper<SysStoreArea>().
                eq(SysStoreArea::getStoreAreaCode, storeAreaBo.getStoreAreaCode()).
                eq(SysStoreArea::getStoreId, storeAreaBo.getStoreId()));
        if (ObjectUtil.isNotNull(store) && store.getStoreId() != storeId) {
            return false;
        }
        return true;
    }

}
